package com.crm.service.statisticAnalysis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.model.common.dtos.PageResult;
import com.crm.model.common.dtos.ResponseResult;
import com.crm.model.common.enums.AppHttpCodeEnum;
import com.crm.model.statisticAnalysis.dtos.ActivityNameWithCountDTO;
import com.crm.model.statisticAnalysis.dtos.ChannelNameWithCountDTO;
import com.crm.model.statisticAnalysis.dtos.ChannelPageDTO;
import com.crm.model.statisticAnalysis.dtos.DoughnutChartDTO;
import com.crm.model.statisticAnalysis.pojos.ChannelStatistics;
import com.crm.model.statisticAnalysis.vos.AnnularAndPieVO;
import com.crm.model.statisticAnalysis.vos.AnnularVO;
import com.crm.model.statisticAnalysis.vos.PieVO;
import com.crm.service.statisticAnalysis.mapper.ChannelMapper;
import com.crm.service.statisticAnalysis.service.ChannelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ChannelServiceImpl implements ChannelService {

    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 渠道分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult channelPage(ChannelPageDTO dto) {
        if (dto == null || dto.getPage() == null || dto.getPageSize() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);

        }
        IPage<ChannelStatistics> page = new Page<>(dto.getPage(), dto.getPageSize());
        LambdaQueryWrapper<ChannelStatistics> lwq = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(dto.getActivityName())) {
            lwq.like(ChannelStatistics::getActivityName, dto.getActivityName());
        }

        if (StringUtils.isNotBlank(dto.getActivityNumber())) {
            lwq.like(ChannelStatistics::getActivityNumber, dto.getActivityNumber());
        }
        page = channelMapper.selectPage(page, lwq);

        PageResult pageResult = new PageResult();
        pageResult.setRecords(page.getRecords());
        pageResult.setTotal(page.getTotal());

        return ResponseResult.okResult(pageResult);
    }

    /**
     * 饼状图+环形图
     *
     * @param queryTime
     * @return
     */
    @Override
    public ResponseResult doughnutChart(LocalDate queryTime) {
        //1.一天最小时间 和 最大时间
        LocalDateTime endTime = LocalDateTime.of(queryTime, LocalTime.MAX);
        LocalDateTime beginTime = LocalDateTime.of(queryTime, LocalTime.MIN);

        //2.查询成交总人数
        Integer bargainCount = channelMapper.selectCount(null);
        //3.查询出活动名称
        List<ActivityNameWithCountDTO> withCountDTOList = channelMapper.getActivityNameWithCount(endTime, beginTime);
        //////////////////////////////////////////
        //10.最终返回集合
        AnnularAndPieVO annularAndPieVO = new AnnularAndPieVO();
        //11.环形图集合
        List<AnnularVO> annularList = new ArrayList<>();
        /////////////////////////////////////////
        //4.活动名称
        String activityNameList = StringUtils.join(withCountDTOList.stream().map(ActivityNameWithCountDTO::getActivityName).collect(Collectors.toList()), ",");
        //5.活动人数
        String activityCountList = StringUtils.join(withCountDTOList.stream().map(ActivityNameWithCountDTO::getActivityCount).collect(Collectors.toList()), ",");

        int size = (int) withCountDTOList.stream().map(ActivityNameWithCountDTO::getActivityCount).count();

        for (int i = 0; i < size; i++) {
            //6.截取活动名称
            String[] activityNameSplit = activityNameList.split(",");
            String activityName = activityNameSplit[i];
            //7.截取活动人数
            String[] activityCountSplit = activityCountList.split(",");
            Integer activityCount = Integer.valueOf(activityCountSplit[i]);

            //8.活动占比 保留两位
            double channelProportion = 0.0;
            if (bargainCount != 0) {
                channelProportion = activityCount.doubleValue() / bargainCount;
            }
            DecimalFormat df = new DecimalFormat("#.00");

            //9.环形图数据对象
            AnnularVO annularVO = new AnnularVO();
            annularVO.setActivityName(activityName);
            annularVO.setActivityProportion(Double.parseDouble(df.format(channelProportion)));

            annularList.add(annularVO);
        }
        //处理饼状图数据
        List<PieVO> pieVOS = pieProcessing(endTime, beginTime);

        //将环形图数据对象存入最终集合中
        annularAndPieVO.setAnnularVO(annularList);
        annularAndPieVO.setPieVO(pieVOS);
        annularAndPieVO.setBargainCount(bargainCount);
        return ResponseResult.okResult(annularAndPieVO);
    }


    private List<PieVO> pieProcessing(LocalDateTime endTime, LocalDateTime beginTime) {

        List<PieVO> pieVOS = new ArrayList<>();

        //查询渠道来源及新增数
        List<ChannelNameWithCountDTO> nameWithCountDTOList = channelMapper.getChannelNameAndCount(endTime, beginTime);
        //渠道名字
        String channelName = StringUtils.join(nameWithCountDTOList.stream().map(ChannelNameWithCountDTO::getChannel).collect(Collectors.toList()), ",");
        //新增数量
        String newChannelCount = StringUtils.join(nameWithCountDTOList.stream().map(ChannelNameWithCountDTO::getChannelCount).collect(Collectors.toList()), ",");
        //渠道总数
        int size = (int) nameWithCountDTOList.stream().map(ChannelNameWithCountDTO::getChannel).count();

        for (int i = 0; i < size; i++) {
            //渠道名称
            String[] channelNameSplit = channelName.split(",");
            String channelname = channelNameSplit[i];

            //新增渠道数
            String[] newChannelCountSplit = newChannelCount.split(",");
            Integer ChannelCount = Integer.valueOf(newChannelCountSplit[i]);

            //渠道占比 新增渠道 / 总渠道数
            double channelProportion = 0.0;
            if (size != 0) {
                channelProportion = ChannelCount.doubleValue() / size;
            }

            PieVO vo = new PieVO();
            vo.setChannel(channelname);
            vo.setChannelProportion(channelProportion);
            pieVOS.add(vo);
        }

        return pieVOS;
    }
}
